package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.base.BasePageQuery;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.Booking;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.vo.BookingVO;
import com.cdck.safe.dete.service.BookingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * (Booking)表控制层
 *
 * @author wp
 * @since 2024-02-25 16:43:26
 */
@Api(value = "booking")
@RestController
@RequestMapping("booking")
@RequiredArgsConstructor
public class BookingController {

    private final BookingService bookingService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams", value = "查询条件")
    })
    public PageResult<BookingVO> queryByPage(BaseKeywordsQuery queryParams) {
        IPage<BookingVO> result = bookingService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "获取预约申请分页列表")
    @GetMapping("/application/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams", value = "查询条件")
    })
    public PageResult<BookingVO> getApplicationPage(BaseKeywordsQuery queryParams) {
        IPage<BookingVO> result = bookingService.getApplicationPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<Booking> getBookingForm(
            @PathVariable Long id
    ) {
        Booking formData = bookingService.getBookingFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param booking 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
            @RequestBody @Valid
                    Booking booking
    ) {
        boolean result = this.bookingService.saveBooking(booking);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateBooking(
            @PathVariable Long id,
            @RequestBody @Validated Booking booking) {
        boolean result = bookingService.updateBooking(id, booking);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteBookings(
            @PathVariable String ids
    ) {
        boolean result = bookingService.deleteBookings(ids);
        return Result.judge(result);
    }

    @ApiOperation(value = "驳回申请")
    @PutMapping(value = "/overrule")
    public Result overrule(
            @RequestBody @Validated Booking booking) {
        boolean result = bookingService.overrule(booking);
        return Result.judge(result);
    }

    @ApiOperation(value = "申请通过")
    @GetMapping(value = "/pass/{id}")
    public Result pass(
            @PathVariable("id") Long id) {
        boolean result = bookingService.pass(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "取消预约")
    @GetMapping(value = "/cancel/{id}")
    public Result cancel(
            @PathVariable("id") Long id) {
        boolean result = bookingService.cancel(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "预约完成")
    @GetMapping(value = "/finish/{id}")
    public Result finish(
            @PathVariable("id") Long id) {
        boolean result = bookingService.finish(id);
        return Result.judge(result);
    }
}

